package com.shujia.day12;

import java.io.*;

public class Demo14Compare {
    public static void main(String[] args) throws IOException {
        /*
            比较各读写方式的效率
         */
        long start = System.currentTimeMillis();// 获取当前系统的毫秒时间
//        readFile1();  // 10分钟
//        readFile2(); // 6.065秒
//        readFile3(); // 0.495秒
        readFile4(); // 7.888秒

        long end = System.currentTimeMillis();
        System.out.println("当前所花费的时间为:"+(end-start)/1000.0+"秒");



    }

    // 大概10分钟左右
    public static void readFile1() throws IOException {
        // 1.普通File类单个字节方式读取
//        ""
        FileInputStream fileInputStream = new FileInputStream("F://data.mp4");
        FileOutputStream fileOutputStream = new FileOutputStream("F://1.mp4");
        int read = -1 ;
        while ((read= fileInputStream.read())!= -1){
            fileOutputStream.write(read);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }

    public static void readFile2() throws IOException {
        // 1.普通File类单个字节方式读取

        FileInputStream fileInputStream = new FileInputStream("F://data.mp4");
        FileOutputStream fileOutputStream = new FileOutputStream("F://2.mp4");
        byte[] bytes = new byte[100];
        int readLen = 0;
        while ((readLen = fileInputStream.read(bytes)) > 0){
            fileOutputStream.write(bytes,0,readLen);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }

    public static void readFile3() throws IOException {
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("F://data.mp4"));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("F://2.mp4"));
        byte[] bytes = new byte[100];
        int readLen = 0;
        while ((readLen = bufferedInputStream.read(bytes)) > 0){
            bufferedOutputStream.write(bytes,0,readLen);
        }
        bufferedOutputStream.close();
        bufferedOutputStream.close();
    }

    public static void readFile4() throws IOException {
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("F://data.mp4"));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("F://2.mp4"));
        int read = -1 ;
        while ((read= bufferedInputStream.read())!= -1){
            bufferedOutputStream.write(read);
        }
        bufferedOutputStream.close();
        bufferedOutputStream.close();
    }



}
